#include <iostream>

using std::cout;
using std::endl;

class Single{
public:
    static Single *getInstance()
    {
        if(_pInstance == nullptr){
            _pInstance = new Single();
        }
        return _pInstance;
    }

    void print(){
        cout << "Single::print()"<<endl;
    }


    Single(const Single & rhs)=delete;
    Single &operator=(const Single & rhs)=delete;

private:
    Single(){}
    ~Single(){}

    class AutoRelease{
    public:
        AutoRelease(){}
        ~AutoRelease(){
            if(_pInstance){
                delete _pInstance;
                _pInstance = nullptr;
            }
        }

    };

private:
    static Single * _pInstance;
    static AutoRelease _ar;

};
Single * Single::_pInstance = nullptr;
Single::AutoRelease Single::_ar;


void test(){
    //Single::getInstance()->print();
    Single *ps1 = Single::getInstance();
    Single *ps2 = Single::getInstance();
    cout << ps1 << endl;
    cout << ps2 << endl;
}

int main()
{
    test();
    return 0;
}

